<script setup lang="ts">
defineProps<{
  visible: boolean
  title: string
  cancelText: string
  confirmText: string
}>()

const emits = defineEmits(['close', 'confirm'])
const closeModal = () => {
  emits('close')
}
const confirmModal = () => {
  emits('confirm','我是子组件')
}
</script>

<template>
  <Teleport to="body">
    <Transition name="modal-fade">
      <div v-if="visible" class="modal-overlay">
        <div class="modal-content">
          <div class="modal-header">
            <h2>{{ title }}</h2>
            <button @click="closeModal">x</button>
          </div>
          <div class="modal-body">
            <slot></slot>
          </div>
          <div class="modal-footer">
            <button @click="closeModal">{{ cancelText }}</button>
            <button @click="confirmModal" class="confirm-btn">{{ confirmText }}</button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.modal-overlay {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal-content {
  width: 400px;
  padding: 20px;
  border-radius: 8px;
  background-color: #fff;
  /* 添加过渡动画，使弹窗缩放变化更平滑 */
  transition: transform 0.3s ease;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.modal-header button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.modal-footer button {
  margin-left: 10px;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.confirm-btn {
  background-color: #1890ff;
  color: white;
}
/* 定义 modal-fade 过渡动画的样式 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}
.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}
.modal-fade-enter-from .modal-content,
.modal-fade-leave-to .modal-content {
  /* 弹窗进入和离开时缩放为 0.9 倍 */
  transform: scale(0.9);
}
</style>
